package com.apply.controller;

import com.apply.config.Results;
import com.apply.entity.SysAnnounce;
import com.apply.service.AnnounceService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tianp
 */
@Slf4j
@RestController
@RequestMapping("/announce/")
public class AnnounceController {
    @Autowired
    private AnnounceService announceService;

    @ApiOperation(value = "获取当前公告")
    @GetMapping("get")
    public Results getAnnounce() {
        SysAnnounce announce = announceService.getAnnounce();
        if (null == announce) {
            return Results.error("公告不存在！");
        }
        return Results.success(announce);
    }

    @ApiOperation(value = "新增/修改公告")
    @PostMapping("saveOrUpdate")
    public Results save(SysAnnounce sysAnnounce) {
        Validate.notNull(sysAnnounce.getAnnounceInfo(), "公告信息不能为空！");
        if (null == sysAnnounce.getId()) {
            announceService.save(sysAnnounce);
            return Results.success(sysAnnounce);
        } else {
            announceService.updateById(sysAnnounce);
            return Results.success(sysAnnounce);
        }
    }
}
